//
//  BaseNavigationController.m
//  侧滑导致的App假死
//
//  Created by chenleping on 2019/3/12.
//  Copyright © 2019年 DSY. All rights reserved.
//

#import "BaseNavigationController.h"

@interface BaseNavigationController ()<UINavigationControllerDelegate, UIGestureRecognizerDelegate>

/**
 全屏侧滑返回手势
 */
@property (nonatomic, strong) UIPanGestureRecognizer * panGesture;

@end

@implementation BaseNavigationController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
//    self.delegate = self;
    
    self.interactivePopGestureRecognizerEnable = YES;
    
    // 添加全屏侧滑返回手势
//    [self.view addGestureRecognizer:self.panGesture];
}

//- (void)navigationController:(UINavigationController *)navigationController didShowViewController:(UIViewController *)viewController animated:(BOOL)animated{
//
//    NSLog(@"%@", NSStringFromSelector(_cmd));
//    self.interactivePopGestureRecognizer.delegate = self;   // 为什么加上这行代码就会出现App假死呢
//    if (navigationController.childViewControllers.count == 1) { // 防止屏幕假死
//        self.interactivePopGestureRecognizer.enabled = NO;
//    }else{
//        self.interactivePopGestureRecognizer.enabled = self.interactivePopGestureRecognizerEnable;
//    }
//}

#pragma mark - UIGestureRecognizerDelegate
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch{
    if (self.childViewControllers.count == 1) { // 防止屏幕假死
        return NO;
    }else{
        return YES;
    }
}

- (void)setPanGestureEnable:(BOOL)panGestureEnable{
    _panGestureEnable = panGestureEnable;
    if (panGestureEnable == YES) {
        [self.view addGestureRecognizer:self.panGesture];
    }else{
        [self.view removeGestureRecognizer:self.panGesture];
    }
}
#pragma mark --- 懒加载
- (UIPanGestureRecognizer *)panGesture{
    if (_panGesture == nil) {
        _panGesture = [[UIPanGestureRecognizer alloc] initWithTarget:self.interactivePopGestureRecognizer.delegate action:@selector(handleNavigationTransition:)];
        _panGesture.delegate = self;
    }
    return _panGesture;
}

@end
